package com.main.usermanagement.interfaces.facade;

import com.main.common.base.resp.RespBody;
import com.main.common.entity.ListPage;
import com.main.usermanagement.application.service.UserService;
import com.main.usermanagement.domain.user.aggregate.User;
import com.main.usermanagement.interfaces.dto.GetUserDTO;
import com.main.usermanagement.interfaces.dto.UserDTO;
import com.main.usermanagement.interfaces.dto.UserListDTO;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Tag(name = "User", description = "系统用户")
@RestController
@RequestMapping("")
public class UserFacade {
    @Resource
    private UserService userService;

    @PostMapping("/getById")
    public RespBody<UserDTO> getById(@Validated @RequestBody GetUserDTO getUserDTO) {
        return userService.getUserById(getUserDTO.getId());
    }


    @PostMapping("/getList")
    public RespBody<UserListDTO> getList(@Validated @RequestBody ListPage listPage) {
        return userService.getList(listPage);
    }


    @PostMapping("/search")
    public RespBody<List<User>> search() {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> andNode = new HashMap<>();
        List<Map<String, Object>> andConditions = new ArrayList<>();

        // 条件1: name LIKE '张'
        Map<String, Object> nameCondition = new HashMap<>();
        nameCondition.put("name", Collections.singletonMap("LIKE", "林"));
        andConditions.add(nameCondition);

        // 条件2: OR(age>25 OR status IN [1,2])
        Map<String, Object> orNode = new HashMap<>();
        List<Map<String, Object>> orConditions = new ArrayList<>();
        orConditions.add(Collections.singletonMap("phone", Collections.singletonMap(">", 2)));
        orConditions.add(Collections.singletonMap("status", Collections.singletonMap("IN", Arrays.asList("INACTIVE", "INACTIVE"))));
        orNode.put("OR", orConditions);
        andConditions.add(orNode);

        andNode.put("AND", andConditions);
        System.out.println(andNode);
        RespBody<List<User>> users = userService.complexQuery(andNode);
        return users;
    }
}
